<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_Installer extends Engine_Package_Installer_Module {
	
	public function addUserProfileBadgesSide() {
		//
		// install content areas
		//
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		// profile page
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'user_profile_index' )->limit ( 1 );
		$page_id = $select->query ()->fetchObject ()->page_id;
		
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'badge.profile-badges' );
		$info = $select->query ()->fetch ();
		if (empty ( $info )) {
			// container_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container_id = $select->query ()->fetchObject ()->content_id;
			
			// left_id (may not always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
			$left_id = $select->query ()->fetchObject ();
			if ($left_id && $left_id->content_id)
				$left_id = $left_id->content_id;
			else
				$left_id = null;
			
			if ($left_id) {
				$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.profile-badges', 'parent_content_id' => $left_id, 'order' => 5, 'params' => '{"title":"","showtitle":1}' ) );
			}
		
		}
	}
	
	protected function getUserProfilePageLeftId($page_id) {
		$db = $this->getDb ();
		
		// container_id (will always be there)
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
		$container_id = $select->query ()->fetchObject ()->content_id;
		
		// left_id (may not always be there)
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
		$left_id = $select->query ()->fetchObject ();
		if ($left_id && $left_id->content_id)
			$left_id = $left_id->content_id;
		else
			$left_id = null;
		
		return $left_id;
	}
	
	public function addUserProfileAwardsSide() {
		//
		// install content areas
		//
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		// profile page
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'user_profile_index' )->limit ( 1 );
		$page_id = $select->query ()->fetchObject ()->page_id;
		$left_id = $this->getUserProfilePageLeftId ( $page_id );
		
		if (! $left_id) {
			return;
		}
		
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'badge.profile-awards' )->where ( 'parent_content_id = ?', $left_id );
		$info = $select->query ()->fetch ();
		
		//echo $select;
		//print_r($info);
		

		if (empty ( $info )) {
			if ($left_id) {
				$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.profile-awards', 'parent_content_id' => $left_id, 'order' => 5, 'params' => '{"title":"","titleCount":false,"max":5,"display_style":"side","showtitle":1,"showdescription":0,"showmeta":0}' ) );
			}
		
		}
	}
	
	public function addUserProfileTab() {
		//
		// install content areas
		//
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		// profile page
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'user_profile_index' )->limit ( 1 );
		$page_id = $select->query ()->fetchObject ()->page_id;
		
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'badge.profile-awards' );
		$info = $select->query ()->fetch ();
		if (empty ( $info )) {
			
			// container_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container_id = $select->query ()->fetchObject ()->content_id;
			
			// middle_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
			$middle_id = $select->query ()->fetchObject ()->content_id;
			
			// tab_id (tab container) may not always be there
			$select->reset ( 'where' )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $page_id )->limit ( 1 );
			$tab_id = $select->query ()->fetchObject ();
			if ($tab_id && @$tab_id->content_id) {
				$tab_id = $tab_id->content_id;
			} else {
				$tab_id = null;
			}
			
			// tab on profile
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.profile-awards', 'parent_content_id' => ($tab_id ? $tab_id : $middle_id), 'order' => 5, 'params' => '{"title":"Awards","titleCount":true,"max":5,"display_style":"tab","showtitle":1,"showdescription":1,"showmeta":1}' ) );
		
		}
	}
	
	public function addHomePage() {
		// Badge Home page
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'badge_index_index' )->limit ( 1 );
		;
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			
			$db->insert ( 'engine4_core_pages', array ('name' => 'badge_index_index', 'displayname' => 'Badge Home Page', 'title' => 'Badge Home Page', 'description' => 'This is the home page for badges.', 'custom' => 0 ) );
			$page_id = $db->lastInsertId ( 'engine4_core_pages' );
			
			$i = 1;
			
			// CONTAINERS (TOP / MAIN)
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'top', 'parent_content_id' => null, 'order' => $i ++, 'params' => '' ) );
			$top_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'main', 'parent_content_id' => null, 'order' => $i ++, 'params' => '' ) );
			$main_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// ---------- CONTAINER TOP & WIDGET MENU -----------
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'middle', 'parent_content_id' => $top_id, 'order' => $i ++, 'params' => '' ) );
			$top_middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.main-menu', 'parent_content_id' => $top_middle_id, 'order' => $i ++, 'params' => '{"title":""}' ) );
			$top_middle_menu_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// ---------- CONTAINER MAIN -----------
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'left', 'parent_content_id' => $main_id, 'order' => $i ++, 'params' => '' ) );
			$main_left_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'right', 'parent_content_id' => $main_id, 'order' => $i ++, 'params' => '' ) );
			$main_right_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'middle', 'parent_content_id' => $main_id, 'order' => $i ++, 'params' => '' ) );
			$main_middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// ------ MAIN :: LEFT WIDGETS  
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.categories', 'parent_content_id' => $main_left_id, 'order' => $i ++, 'params' => '{"title":"","photo":1,"details":0,"descriptionlength":68}' ) );
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.list-awards', 'parent_content_id' => $main_left_id, 'order' => $i ++, 'params' => '{"title":"Recent Awards","max":5,"showphoto":1,"showdetails":1}' ) );
			
			// ------ MAIN :: RIGHT WIDGETS
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.search-form', 'parent_content_id' => $main_right_id, 'order' => $i ++, 'params' => '{"title":""}' ) );
			
			// ------ MAIN :: MIDDLE WIDGETS   
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.featured-award', 'parent_content_id' => $main_middle_id, 'order' => $i ++, 'params' => '{"title":"Featured Award"}' ) );
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'badge.list-badges', 'parent_content_id' => $main_middle_id, 'order' => $i ++, 'params' => '{"title":"Recent Badges","max":20,"order":"recent","display_style":"wide","showphoto":1,"showdetails":1}' ) );
		
		}
	}
	
	public function onInstall() {
		$this->addUserProfileTab ();
		$this->addUserProfileAwardsSide ();
		$this->addUserProfileBadgesSide ();
		
		$this->addHomePage ();
		
		parent::onInstall ();
	}
}
